package cn.piesat.dao;

import cn.piesat.dto.PiesatUserInfoDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * 用户基本信息持久层
 *
 * @author xhl
 */
public interface PiesatUserInfoDAO extends JpaRepository<PiesatUserInfoDTO,String> {

    /**
     * 将用户的部门ID修改为null
     * @param deptIdList
     */
    @Modifying
    @Query("update PiesatUserInfoDTO set deptId = null where deptId in (:deptIdList)")
    void updateDeptIn(List<String> deptIdList);

    PiesatUserInfoDTO findByUsername(String useranem);

    PiesatUserInfoDTO findByMobile(String mobile);

    PiesatUserInfoDTO findByEmail(String email);

    List<PiesatUserInfoDTO> findAllByLockState(Integer lockState);

    /**
     * 查询最后登录时间前十的信息
     * @return
     */
    @Query(value = "select s.USERNAME,s.LAST_LOGIN_TIME from piesat_user_userinfo s where s.LAST_LOGIN_TIME is not null order by s.LAST_LOGIN_TIME desc limit 0,10",nativeQuery = true)
    List<Object[]> findRecentUser();

    Long countAllByLastLoginTimeBetween(Date todayD, Date tomorrowD);

    @Query(nativeQuery = true,value = "select USERNAME from piesat_user_userinfo where LOCK_STATE = 0")
    List<String> listAllUserNames();
}
